package com.jfinal.weixin.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.weixin.entity.DianYing;
import org.jsoup.Jsoup;

import java.util.ArrayList;

public class DianYingUtil {
    public static ArrayList searchDianYing(String searchName){
        ArrayList list=new ArrayList();

        try {

            int pg=1;
            int pageCount=0;
            do{
                String search_url="http://api.tiankongapi.com/api.php/provide/vod/?ac=list&wd="+searchName+"&pg="+pg;
                String str= Jsoup.connect(search_url).ignoreContentType(true).get().body().text();

                System.out.println(str);

                JSONObject jsonObject=JSONObject.parseObject(str);
//                int total=jsonObject.getInteger("total");
                pageCount=jsonObject.getInteger("pagecount");

                JSONArray jsonArray=jsonObject.getJSONArray("list");
                for (int i=0;i<jsonArray.size();i++){
                    JSONObject dy_json=jsonArray.getJSONObject(i);
                    DianYing dy=new DianYing(dy_json.getString("vod_id"),dy_json.getString("vod_name"),dy_json.getString("vod_play_url"));

                    list.add(dy);
                }
                pg++;
            }while (pg<=pageCount);


        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }



    public static String searchDianYingGetStr(String searchName){
        String dianyingstr="";

        try {

            int pg=1;
            int pageCount=0;
            do{
                String search_url="http://api.tiankongapi.com/api.php/provide/vod/?ac=list&wd="+searchName+"&pg="+pg;
                String str= Jsoup.connect(search_url).ignoreContentType(true).get().body().text();

                System.out.println(str);

                JSONObject jsonObject=JSONObject.parseObject(str);
//                int total=jsonObject.getInteger("total");
                pageCount=jsonObject.getInteger("pagecount");

                JSONArray jsonArray=jsonObject.getJSONArray("list");
                for (int i=0;i<jsonArray.size();i++){
                    JSONObject dy_json=jsonArray.getJSONObject(i);
                    DianYing dy=new DianYing(dy_json.getString("vod_id"),dy_json.getString("vod_name"),dy_json.getString("vod_play_url"));
                    dianyingstr+=dy.getVod_id()+",\t"+dy.getVod_name()+"\n";


                }
                pg++;
            }while (pg<=pageCount);


        }catch (Exception e){
            e.printStackTrace();
        }

        return dianyingstr;
    }


    public static DianYing getDetail(String vod_id)
    {
        DianYing dy=new DianYing();

        try {

            int pg=1;
            int pageCount=0;
                String search_url="http://api.tiankongapi.com/api.php/provide/vod/?ac=detail&ids="+vod_id;
                String str= Jsoup.connect(search_url).ignoreContentType(true).get().body().text();


                JSONObject jsonObject=JSONObject.parseObject(str);
                int total=jsonObject.getInteger("total");
                pageCount=jsonObject.getInteger("pagecount");

                if (total>0) {

                    JSONObject dy_json = jsonObject.getJSONArray("list").getJSONObject(0);


                    dy = new DianYing(dy_json.getString("vod_id"), dy_json.getString("vod_name"), dy_json.getString("vod_play_url"));
                }



        }catch (Exception e){
            e.printStackTrace();
        }

        return dy;
    }
}
